package com.tlcx.kfip.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tlcx.kfip.R;
import com.tlcx.kfip.adapter.base.BaseAdapter;
import com.tlcx.kfip.entity.LastAnnounceEntity;
import com.tlcx.kfip.ui.CustomCountDownView;
import com.tlcx.kfip.utils.AppInfoUtils;

/**
 * Created by victor on 2016/10/6 21:38.
 * Email:zhuzhaoyang@yuanchuangyun.com
 */
public class LastAnnounceListAdapter extends BaseAdapter<LastAnnounceEntity> {

    private Context mContext;
    private CustomCountDownView countdownView;
    private TextView winnerTv;
    private TextView participatorCountTv;
    private TextView announcedTimeTv;
    private SimpleDraweeView avatarView;

    public LastAnnounceListAdapter(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public int getItemResource(int viewType) {
        return R.layout.adapter_last_announce;
    }

    @Override
    public View getItemView(int position, View convertView, ViewHolder viewHolder) {
        LastAnnounceEntity item = getItem(position);

        if (item == null) {
            return convertView;
        }

        //设置标题名字
        TextView title = viewHolder.getView(R.id.tv_adapter_last_announce_name);
        title.setText(item.getName());

        //设置icon
        SimpleDraweeView draweeView = viewHolder.getView(R.id.iv_adapter_last_announce_icon);
        Uri uri = Uri.parse("res://" + AppInfoUtils.getCurrentPkgName(mContext) + "/" + item.getIconUrl());
        draweeView.setImageURI(uri);

        //设置价格
        TextView priceTv = viewHolder.getView(R.id.tv_adapter_last_announce_price);
        priceTv.setText(mContext.getString(R.string.last_announce_tip_price) + item.getPrice());

        winnerTv = viewHolder.getView(R.id.tv_adapter_last_announce_winner);
        participatorCountTv = viewHolder.getView(R.id.tv_adapter_last_announce_participator_count);
        announcedTimeTv = viewHolder.getView(R.id.tv_adapter_last_announce_announce_time);
        avatarView = viewHolder.getView(R.id.sdv_adapter_last_announce_avatar);
        countdownView = viewHolder.getView(R.id.ccdv_adapter_last_announce);

        //设置中奖者
        winnerTv.setText(setWinnerStyle(item.getWinner()));

        //设置参与人数
        participatorCountTv.setText(setParticipatorCountStyle(item.getPersonCount()));

        //设置揭晓时间
        announcedTimeTv.setText(mContext.getString(R.string.last_announce_tip_announced_time)+item.getTime());

        //设置用户头像
        Uri avatarUri = Uri.parse("res://" + AppInfoUtils.getCurrentPkgName(mContext) + "/" + item.getIconUrl());
        avatarView.setImageURI(avatarUri);

        //如果开奖字段剩余时间大于0，则显示倒计时，否则显示中奖者相关信息
        if(item.getRemainTime()<= 0){
            setCountDownEnd();
        }else {
            //设置倒计时
            setCountDownStart(item);
        }


        return convertView;
    }

    /**
     * 设置倒计时开始时，哪些内容显示，哪些内容隐藏
     */
    private void setCountDownStart(LastAnnounceEntity item){
        winnerTv.setVisibility(View.GONE);
        participatorCountTv.setVisibility(View.GONE);
        announcedTimeTv.setVisibility(View.GONE);
        avatarView.setVisibility(View.GONE);
        countdownView.setVisibility(View.VISIBLE);
        countdownView.setCountDownTimer(item.getEndTime() - System.currentTimeMillis(), new CustomCountDownView.OnCountDownFinishListener() {
            @Override
            public void onFinish() {
                setCountDownEnd();
            }
        });
    }

    /**
     * 设置倒计时结束时，哪些内容显示,哪些内容隐藏
     */
    private void setCountDownEnd(){
        winnerTv.setVisibility(View.VISIBLE);
        participatorCountTv.setVisibility(View.VISIBLE);
        announcedTimeTv.setVisibility(View.VISIBLE);
        avatarView.setVisibility(View.VISIBLE);
        countdownView.setVisibility(View.GONE);
        notifyDataSetChanged();
    }
    /**
     * 设置中奖者显示样式
     */
    private Spanned setWinnerStyle(String winnerName) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#a0a0a0'>");
        buffer.append(mContext.getString(R.string.last_announce_tip_winner));
        buffer.append("</font>");
        buffer.append("<font color = '#38b1ff'>");
        buffer.append(winnerName);
        buffer.append("</font>");
        return  Html.fromHtml(buffer.toString());
    }

    /**
     * 设置参与人次显示样式
     */
    private Spanned setParticipatorCountStyle(int participatorCount){
        StringBuffer buffer = new StringBuffer();
        buffer.append("<font color = '#a0a0a0'>");
        buffer.append(mContext.getString(R.string.last_announce_tip_participator_count_prefix));
        buffer.append("</font>");
        buffer.append("<font color = '#e55c77'>");
        buffer.append(participatorCount);
        buffer.append("</font>");
        buffer.append("<font color = '#a0a0a0'>");
        buffer.append(mContext.getString(R.string.last_announce_tip_participator_count_suffix));
        buffer.append("</font>");

        return  Html.fromHtml(buffer.toString());
    }

}
